package main

import (
	"net/http"
	"io/ioutil"
	"fmt"
	"strings"
	"encoding/json"
)

func main() {
	organs := getOrgans()

	for _, organ := range organs {
		fmt.Println("organ:", organ.OTC_ID, organ.OTC_NAME)

		companys := getCompanys(organ.OTC_ID);
		for _, company := range companys {
			fmt.Println("company:", company)

			aoiType := "0"
			aoiTypeId := getAOIType(company.AOI_ID);
			if aoiTypeId == "05" {
				aoiType = "1"
			}

			persons := getPersons(aoiType,company.AOI_ID)
			for _, person := range persons {
				fmt.Println("person:", person)
			}
		}
	}

	fmt.Println("--------------")
}

type Organ struct {
	OTC_ID   string `json:OTC_ID`
	OTC_NAME string
	OTC_DESC string
}

type CompanyInfo struct {
	AutoCount bool `json:autoCount`
	Result    []Company `json:result`
}

type Company struct {
	RNUM            int
	AOI_ID          string
	AOI_NAME        string `json:AOI_NAME`
	PR_COUNT_PERSON string

	PTI0PERSON  string
	PTI1PERSON  string
	PTI2PERSON  string
	PTI3PERSON  string
	PTI4PERSON  string
	PTI5PERSON  string
	PTI6PERSON  string
	PTI7PERSON  string
	PTI8PERSON  string
	PTI9PERSON  string
	PTI10PERSON string
}

type Person struct {
	AOI_NAME     string
	CER_NUM      string
	COUNTCER     string
	COUNTCX      string
	CTI_NAME     string
	ECO_NAME     string
	PPP_END_DATE string
	PPP_GET_DATE string
	PTI_NAME     string
	RPI_ID       string
	RPI_NAME     string
	SCO_NAME     string
}

func getOrgans() []Organ {
	client := &http.Client{}

	req, err := http.NewRequest("POST",
		"http://person.amac.org.cn/pages/registration/train-line-register!search.action",
		strings.NewReader("sqlkey=registration&sqlval=ORG_TYPE_CODE"))
	if err != nil {
		// handle error
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//req.Header.Set("Cookie", "name=anny")

	resp, err := client.Do(req)

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(string(body))

	var organs []Organ
	json.Unmarshal(body, &organs)

	return organs
}

func getCompanys(oganId string) []Company {
	client := &http.Client{}

	var params = "filter_LIKES_AOI_NAME=&page.searchFileName=registration&page.sqlKey=SELECT_LINE_PERSON_LIST" +
		"&page.sqlCKey=SELECT_LINE_PERSON_SIZE&page.pageSize=500&page.pageNo=1&page.orderBy=AOI.AOI_NAME" +
		"&page.order=desc&_search=false&nd=1529171299876&filter_EQS_OTC_ID=" + oganId

	req, err := http.NewRequest("POST",
		"http://person.amac.org.cn/pages/registration/train-line-register!list.action",
		strings.NewReader(params))
	if err != nil {
		// handle error
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//req.Header.Set("Cookie", "name=anny")

	resp, err := client.Do(req)

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		// handle error
	}

	//fmt.Println(string(body))

	var companyinfo CompanyInfo
	json.Unmarshal(body, &companyinfo)

	return companyinfo.Result
}

func getAOIType(AOI_ID string) string {
	client := &http.Client{}

	var params = "sqlkey=registration&sqlval=SERAOI_TYPE&filter_EQS_AOI_ID=" + AOI_ID

	req, err := http.NewRequest("POST",
		"http://person.amac.org.cn/pages/registration/train-line-register!search.action",
		strings.NewReader(params))
	if err != nil {
		// handle error
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//req.Header.Set("Cookie", "name=anny")

	resp, err := client.Do(req)

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		// handle error
	}

	//fmt.Println(string(body))

	var array []map[string]string
	json.Unmarshal(body, &array)

	return array[0]["AOI_TYPE_ID"]
}

func getPersons(AOI_TYPE string, AOI_ID string) []Person {
	client := &http.Client{}

	var params = "sqlkey=registration&sqlval=SEARCH_FINISH_PUBLICITY&ORDERNAME=PP#PTI_ID,PP#PPP_NAME&ORDER=ASC" +
		"&filter_EQS_AOI_ID=" + AOI_ID + "&filter_EQS_PTI_ID=" + AOI_TYPE
	req, err := http.NewRequest("POST",
		"http://person.amac.org.cn/pages/registration/train-line-register!search.action",
		strings.NewReader(params))
	if err != nil {
		// handle error
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//req.Header.Set("Cookie", "name=anny")

	resp, err := client.Do(req)

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		// handle error
	}

	//fmt.Println("##"+string(body))

	var persons []Person
	json.Unmarshal(body, &persons)

	return persons
}
